package com.slf.engine.services.impl;

import java.sql.SQLException;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import com.slf.engine.base.IBaseDao;
import com.slf.engine.bo.Order;
import com.slf.engine.common.SysContext;
import com.slf.engine.services.IBossService;
import com.slf.engine.services.IReloadService;
import com.slf.engine.services.helper.ServiceHelper;
import com.slf.engine.utils.Factory;

public class ReloadServiceImpl implements IReloadService {

	private static final Logger log = Logger.getLogger(ReloadServiceImpl.class);
	
	public ReloadServiceImpl()
	{
		log.log(Priority.INFO, "===重载服务启动===");
	}
	/**
	 * 重载未发送订单
	 */
	public boolean reloadUnSendOrder()
	{
		IBaseDao baseDao = (IBaseDao)Factory.getInstantBiz("baseDao");
		try {
			List<Order> list = baseDao.getList("reloadUnSendOrders");
			log.log(Priority.INFO, "已加载未发送订单[" + list.size() + "]");
			IBossService bossService = new BossServiceImpl();
			for(int i = 0 ; i < list.size(); i++)
			{
				Order order = list.get(i);
				bossService.doPost(order);
				SysContext.ORDER_LIST.add(order);
			}
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			log.log(Priority.ERROR, "数据库操作失败");
			return false;
		} catch (Exception e) {
			// TODO: handle exception
			log.log(Priority.ERROR, "数据库连接失败--3秒钟后重试");
		}
		return false;
	}
	
	/**
	 * 重载未通知订单
	 */
	public boolean reloadUnNoticeOrder()
	{
		IBaseDao baseDao = (IBaseDao)Factory.getInstantBiz("baseDao");
		try {
			List<Order> list = baseDao.getList("reloadNoticeOrders");
			log.log(Priority.INFO, "已加载未通知订单[" + list.size() + "]");
			SysContext.NOTICE_LIST.addAll(list);
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			log.log(Priority.ERROR, "数据库操作失败");
			return false;
		} catch (Exception e) {
			
			// TODO: handle exception
			log.log(Priority.ERROR, "数据库连接失败--3秒钟后重试");
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
			}
			return reloadUnNoticeOrder();
		}
	}
	
//	/**
//	 * 重载未查询订单
//	 */
//	public boolean reloadUnQueryOrder()
//	{
//		IBaseDao baseDao = (IBaseDao)Factory.getInstantBiz("baseDao");
//		try {
//			List<Order> list = baseDao.getList("reloadUnBackOrders");
//			log.log(Priority.INFO, "已加载未查询订单[" + list.size() + "]");
//			for(int i = 0; i < list.size(); i++)
//			{
//				Order order = list.get(i);
//				ServiceHelper.putToCache(order);
//			}
//			return true;
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			log.log(Priority.ERROR, "数据库操作失败");
//			return false;
//		} catch (Exception e) {
//			
//			// TODO: handle exception
//			log.log(Priority.ERROR, "数据库连接失败--3秒钟后重试");
//			try {
//				Thread.sleep(3000);
//			} catch (InterruptedException e1) {
//				// TODO Auto-generated catch block
//			}
//			return reloadUnNoticeOrder();
//		}
//	}
}
